package cmbchina.util;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class AppConfig {

    public static final String DEFAULT_APP_CONFIG = "application.properties";
    private static Logger logger = LoggerFactory.getLogger(AppConfig.class);
    private static AppConfig instance = null;
    private static long startupTimestamp = System.currentTimeMillis();
    private Properties appConfig;


    private AppConfig() {
    }

    public static AppConfig getInstance() {
        if (instance == null)
            instance = new AppConfig();
        return instance;
    }

    public Properties getConfig() {
        if (appConfig == null) {
            String filename = "";
            try {
                filename = DEFAULT_APP_CONFIG;
                String path = AppConfig.class.getClassLoader().getResource(DEFAULT_APP_CONFIG).getPath();
                FileInputStream in = new FileInputStream(path);
                appConfig = new Properties();
                appConfig.load(in);
                appConfig.getProperty("username");
//                appConfig = new PropertiesConfiguration(filename);
                logger.info(filename + " loaded");
            } catch (Exception e) {
                logger.error("Cannot load config file " + filename);
//                throw new ExceptionInInitializerError(String.valueOf(e));
            }
        }
        return appConfig;
    }




    /**
     * Get CMB SharePoint integration info
     */
    public Map<String, String> getItilInfo()  {
        Map<String, String> result = new HashMap<String, String>();
        result.put("itil_url", getConfig().getProperty("itil.url"));
        result.put("itil_username", getConfig().getProperty("itil.username"));
        result.put("itil_password", getConfig().getProperty("itil.password"));
        result.put("itil_port", getConfig().getProperty("itil.port"));
        result.put("itil_timeout", getConfig().getProperty("itil.timeout"));
        result.put("itil_protocol", getConfig().getProperty("itil.protocol"));
        return result;
    }

    /**
     * Get cloud SharePoint integration info
     */
    public Map<String, String> getCloudInfo() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("cloud.url", getConfig().getProperty("cloud.url"));
        result.put("cloud.accessKey", getConfig().getProperty("cloud.accessKey"));
        result.put("cloud.securityKey", getConfig().getProperty("cloud.accessKey"));
        return result;
    }

    /**
     * Get cloud SharePoint integration info
     */
    public Map<String, String> getCloudQureyInfo() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("cloud.query.url", getConfig().getProperty("cloud.query.url"));
        result.put("cloud.query.accessKey", getConfig().getProperty("cloud.query.accessKey"));
        result.put("cloud.query.securityKey", getConfig().getProperty("cloud.query.accessKey"));
        return result;
    }

    /**
     * Get value using key from properties file
     *
     * @param name key
     * @return value
     */
    public String getPropertiesValueByName(String name) {
        return getConfig().getProperty(name).toString();
    }

    public static void main(String [] args){
        String pas=AppConfig.getInstance().getPropertiesValueByName("cloud.url").trim();

//        // 需要加密的字符串
//        String email = "hpadmin_20121";
//        // 加密
//        String encrypted = DESUtils.encode(email);
//        // 解密
//        String decrypted = DESUtils.decode(encrypted);
//        // 输出结果;
//        System.out.println("email: " + email);
//        System.out.println("encrypted: " + encrypted);
//        System.out.println("encrypte1: "+pas);
//        System.out.println("decrypted: " + decrypted);
//        System.out.println("email.equals(decrypted): " + email.equals(decrypted));


        if(pas.indexOf("[")!=-1){

            pas=pas.substring(1,pas.length()-1);
            System.out.print("xxxxxxx");
        }
        System.out.print(pas);


    }
}